<template>
	<view class="zy-column-full">
		<zy-title rightText="发布" @right="requestSave"></zy-title>
		
		<view class="zy-page-padding">
			<view class="zy-text-white-lg-bold zy-mt-lg">上传生活照</view>
			
			<view class="zy-mt-50">
				<uni-file-picker limit="3" @select="selectPhoto" @delete="deletePhoto" ref="files"
					:auto-upload="false" :value="photos"></uni-file-picker>
			</view>
		</view>
	</view>
</template>

<script>
	import { uploadOSS } from '@/js_sdk/jason-alioss-upload/oss.js'
	import RouteUtil from '@/main/common/RouteUtil';
	export default {
		data() {
			return {
				form: {
					type: '1', //"1图片 2视频 3文本"
					content: '',
					fileIds: ''
				},
				user:{},
				photos: [],
				currentPicIndex: 0,
				introPicIds: '',
				
			};
		},
		onLoad() {
			this.requestUser();
		},
		methods: {
			requestUser() {
				uni.$http.getUser().then(res => {
					this.user = res.data;
					//形象照
					if (this.user.introPicIdUrl) {
						let ids = this.user.introPicIds.split(',')
						this.user.introPicIdUrl.forEach((item, index) => {
							this.photos.push({
								url: item,
								id: ids[index]
							});
						});
					}
				})
			},
			// 新增图片
			selectPhoto(event) {
				this.currentPicIndex = 0
				event.tempFilePaths.forEach((file, index) => {
					let dir = 'userinfo'
					uploadOSS(dir, file).then(res => {
						this.requestFilePhoto(dir, res.fileName, res.picUrl, index, event.tempFilePaths.length)
					}).catch(err=>{
						this.currentPicIndex++;
					})
				});
			},
			requestFilePhoto(pathKey, fileNameKey, picUrl, index, length) {
				let params = {
					pathKey: pathKey,
					userIdKey: this.user.id,
					fileNameKey: fileNameKey
				}
				uni.$http.post('/api/ossSts/callBackFiInfo', params).then(res=>{
					this.currentPicIndex++;
					this.photos.push({
						url: picUrl,
						id: res.data
					});
					if (this.introPicIds) {
						this.introPicIds += ',' + res.data;
					} else {
						this.introPicIds = res.data;
					}
					if (this.currentPicIndex == length) {
						this.requestUploadPhoto(1)
					}
				}).catch(res=>{
					this.currentPicIndex++;
				})
			},
			async requestUploadPhoto(useFileType) {
				let params = {
					useFileType: useFileType,
					introPicIds: this.introPicIds,
					id: this.user.id
				}
				uni.$http.updateUser(params)
			},
			async deletePhoto(event) {
				this.introPicIds = ''
				this.photos = this.photos.filter(item => {
					if (item.url === event.tempFilePath) {
						this.introPicIds = item.id
					}
					return item.url !== event.tempFilePath;
				})
				this.requestUploadPhoto(2)
			},

			requestSave() {
				uni.$toast.showToast('上传成功')
				RouteUtil.back()
			}
		}
	}
</script>

<style lang="scss">
	.parent {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #fff;
	}

	.content {
		width: 100wh;
		height: 200rpx;
		border: 1rpx solid #efefef;
		margin: 30rpx;
		padding: 20rpx;
		border-radius: 20rpx;
	}

	.photo {
		background-color: #fff;
		margin: 30rpx 0;
		padding: 30rpx;
	}

	.bottom {
		margin: 30rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		background-image: linear-gradient(to right, #9fe9ff, #ae5eff);
		border-radius: 40rpx;
		color: #fff;
	}
</style>